package cn.wzut.edut.dao.pojo.table;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyTeacherMapper;
import cn.wzut.edut.dao.mapper.SysUserMapper;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TeacherInfListener extends AnalysisEventListener<TeacherInf> {
    List<TeacherInf> list = new ArrayList<TeacherInf>();
    List<SysUser> sysUsers = new LinkedList<>();
    List<PtyTeacher> ptyTeachers = new LinkedList<>();
    List<SysDeptUserRel> sysDeptUserRels = new LinkedList<>();

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     */
    public TeacherInfListener() {}
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    PtyTeacherMapper ptyTeacherMapper;
    SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data
     * @param context
     */
    @Override
    public void invoke(TeacherInf data, AnalysisContext context) {
        System.out.print("解析到一条数据：");
        System.out.println(data);
        System.out.println(snowflakeIdWorker);
        String s1= String.valueOf(snowflakeIdWorker.nextId());
        System.out.println(s1);
        String s2= String.valueOf(snowflakeIdWorker.nextId());
        System.out.println(s2);
        String s3= String.valueOf(snowflakeIdWorker.nextId());
        if(data!=null){
            SysUser sysUser = new SysUser();
            PtyTeacher ptyTeacher = new PtyTeacher();
            SysDeptUserRel sysDeptUserRel = new SysDeptUserRel();
            sysUser.setId(s1);
            sysUser.setUserName(data.getUserName());
            sysUser.setRealName(data.getRealName());
            sysUser.setHashPassword("e10adc3949ba59abbe56e057f20f883e");
            sysUser.setDept(data.getDept());
            sysUser.setDelFlag(data.getDelFlag());
            ptyTeacher.setId(s2);
            ptyTeacher.setUserId(s1);
            ptyTeacher.setSex(data.getSex());
            ptyTeacher.setOpenId(data.getOpenId());
            ptyTeacher.setPhone(data.getPhone());
            sysDeptUserRel.setDeptId(data.getDept());
            sysDeptUserRel.setUserId(s1);
            sysDeptUserRel.setId(s3);
            sysUsers.add(sysUser);
            ptyTeachers.add(ptyTeacher);
            sysDeptUserRels.add(sysDeptUserRel);
        }
        list.add(data);
    }
    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
//        sysUserMapper.insertBatchSomeColumn(sysUsers);
//        ptyTeacherMapper.insertBatchSomeColumn(ptyTeachers);
        System.out.println(list);
    }
    /**
     * 返回数据
     *
     * @return 返回读取的数据集合
     **/
    public List<SysUser> getSysUsers() {
        return sysUsers;
    }
    /**
     * 返回数据
     *
     * @return 返回读取的数据集合
     **/
    public List<PtyTeacher> getPtyTeachers() {
        return ptyTeachers;
    }
    /**
     * 返回数据
     *
     * @return 返回读取的数据集合
     **/
    public List<SysDeptUserRel> getSysDeptUserRels() {
        return sysDeptUserRels;
    }
}
